//<![CDATA[

// Assign Icon to specific type
var customIcons = [];

customIcons["iconCollege"] = iconCollege;

/**Year 1 - Colour: Blue*/
customIcons["Klas1B1"] = Klas1B1;
customIcons["Klas1B2"] = Klas1B2;
customIcons["Klas1B3"] = Klas1B3;
customIcons["Klas1H1"] = Klas1H1;
customIcons["Klas1H2"] = Klas1H2;
customIcons["Klas1M1"] = Klas1M1;
customIcons["Klas1M2"] = Klas1M2;
customIcons["Klas1M3"] = Klas1M3;
customIcons["Klas1V1"] = Klas1V1;
customIcons["Klas1V2"] = Klas1V2;

/**Year 2 - Colour: White*/
customIcons["Klas2H1"] = Klas2H1;
customIcons["Klas2H2"] = Klas2H2;
customIcons["Klas2H3"] = Klas2H3;
customIcons["Klas2M1"] = Klas2M1;
customIcons["Klas2M2"] = Klas2M2;
customIcons["Klas2M3"] = Klas2M3;
customIcons["Klas2M4"] = Klas2M4;
customIcons["Klas2V1"] = Klas2V1;
customIcons["Klas2V2"] = Klas2V2;
customIcons["Klas2V3"] = Klas2V3;

/**Year 3 - Colour: Yellow*/
customIcons["Klas3H1"] = Klas3H1;
customIcons["Klas3H2"] = Klas3H2;
customIcons["Klas3H3"] = Klas3H3;
customIcons["Klas3M1"] = Klas3M1;
customIcons["Klas3M2"] = Klas3M2;
customIcons["Klas3M3"] = Klas3M3;
customIcons["Klas3V1"] = Klas3V1;
customIcons["Klas3V2"] = Klas3V2;

/**Year 4 - Colour: Orange*/
customIcons["Klas4H1"] = Klas4H1;
customIcons["Klas4H2"] = Klas4H2;
customIcons["Klas4M1"] = Klas4M1;
customIcons["Klas4M2"] = Klas4M2;
customIcons["Klas4M3"] = Klas4M3;
customIcons["Klas4V1"] = Klas4V1;
customIcons["Klas4V2"] = Klas4V2;

/**Year 5 - Colour: Green*/
customIcons["Klas5H1"] = Klas5H1;
customIcons["Klas5H2"] = Klas5H2;
customIcons["Klas5V1"] = Klas5V1;
customIcons["Klas5V2"] = Klas5V2;

/**Year 6 - Colour: Red*/
customIcons["Klas6V1"] = Klas6V1;
customIcons["Klas6V2"] = Klas6V2;


/**Loads google maps with settings
 * Settings: - Default center of map: DKC
 *           - Default Zoom: lvl 13
 *           - Default Maptype: Normal map
 *           - Min-Zoom: 11
 *           - Max-Zoom: 19
 *           - Zoom with scrollWheel: enabled
 *           - mapRotation: enabled
 * 
 */
function load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(52.022500, 4.7220571), 13);
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		G_NORMAL_MAP.getMinimumResolution = function () { return 11 };
		G_NORMAL_MAP.getMaximumResolution = function () { return 19 };
		map.enableScrollWheelZoom();
		map.enableRotation();
		for ( var i = 0; i < store_locations.length; i++) {
			var type = store_locations[i][0];
			var point = new GLatLng(parseFloat(store_locations[i][1]),
					parseFloat(store_locations[i][2]));
			var address = store_locations[i][4];
			var studentID = store_locations[i][5];
			//TODO: Let op store_locations.length!!
			var marker = createMarker(point, store_locations[i][3], address,
					type, studentID);
			map.addOverlay(marker);
		}
		if(GMap2.isLoaded()){
			if(counterLoaded == 0)
			{
				toggleGroup('Klas2M1');
				counterLoaded++;
			}
		}
	}
}

/**
 * Input: point - GLatlng made with lattitude and longitude from parents residence
 *        name  - Name of student
 *        address - Address of parents residence
 *        type  - classCode
 * Makes a new marker.
 */
function createMarker(point, name, address, type,studentID) {
	var marker = new GMarker(point, customIcons[type]);
	markerGroups[type].push(marker);
//	console.log(type);
	if (type == "iconCollege") {
		var html = "<b> Donald Knuth College</b> <br/><img src='images/DKC.png'/>";
	} else {
//		var html = "<b>" + name + "</b><u><br/>"+type+"</u> <br/>" + address;
		var html = "<b>" + name + "</b><u><br/>"+type+"</u> <br/>" + address+"<br/> <a href=\"statsSelect.jsp?student_nummer="+studentID+"\">Overzicht cijfers</a>";
	}
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});
//	console.log(marker);
	return marker;
}

/** INPUT: type - Classcode
 * toggleGroup the subarray of MarkersGroup, and toggles each marker
 */
function toggleGroup(type) {
	for ( var i = 0; i < markerGroups[type].length; i++) {
		var marker = markerGroups[type][i];
		if (marker.isHidden()) {
			marker.show();
		} else {
			marker.hide();
		}

	}
}
// ]]>
